package com.android.videotest;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import java.util.Arrays;

import cn.jzvd.JZUtils;

public class Utils {
    /**
     * 判断是否平板设备
     *
     * @param context
     * @return true:平板,false:手机
     */
    private boolean isTabletDevice(Context context) {
        return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }

    //设置屏幕方向
    private void setScreenOrientation(Activity activity) {
        if (!isTabletDevice(activity)) {
            //手机，竖屏
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else {
            //平板，横屏
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }

    public static void fullScreen(View view) {
        ViewGroup parent = (ViewGroup) view.getParent();
        parent.removeView(view);
        final ViewGroup decorView = (ViewGroup) ((Activity) view.getContext()).getWindow().getDecorView();
        AnimatorSet animatorSet = new AnimatorSet();
        PropertyValuesHolder holder3 = PropertyValuesHolder.ofInt("width", view.getWidth(), decorView.getHeight());
        PropertyValuesHolder holder4 = PropertyValuesHolder.ofInt("height", view.getHeight(), decorView.getWidth());
        PropertyValuesHolder holder5 = PropertyValuesHolder.ofInt("rotation", 0, 90);
        boolean rotation = false;
        if (view.getWidth() > view.getHeight()) {
            rotation = true;
        }
        int[] outLocation = new int[2];
        view.getLocationInWindow(outLocation);
        Log.d("-App", "fullScreen: " + Arrays.toString(outLocation));
//        outLocation[0]
        ValueAnimator valueAnimator = ObjectAnimator.ofPropertyValuesHolder(holder3, holder4, holder5);
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(decorView.getHeight(), decorView.getWidth());
                params.gravity = Gravity.CENTER;
                decorView.addView(view, params);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                JZUtils.hideStatusBar(view.getContext());
                JZUtils.hideSystemUI(view.getContext());
            }
        });
        final boolean finalRotation = rotation;
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int width = (int) animation.getAnimatedValue("width");
                int height = (int) animation.getAnimatedValue("height");
                Log.d(this.getClass().getSimpleName() + "-App", "onAnimationUpdate: " + height);
                ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                layoutParams.width = width;
                layoutParams.height = height;
                view.setLayoutParams(layoutParams);
                if (finalRotation) {
                    int rotation = (int) animation.getAnimatedValue("rotation");
                    view.setRotation(rotation);
                }
            }
        });
        animatorSet.playTogether(valueAnimator);
        animatorSet.setDuration(800);
        animatorSet.start();
    }

    public static void fullScreen1(View view) {
        ViewGroup parent = (ViewGroup) view.getParent();
        parent.removeView(view);
        Activity activity = (Activity) view.getContext();
        final ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
        AnimatorSet animatorSet = new AnimatorSet();
        PropertyValuesHolder holder3 = PropertyValuesHolder.ofInt("width", view.getWidth(), decorView.getHeight());
        PropertyValuesHolder holder4 = PropertyValuesHolder.ofInt("height", view.getHeight(), decorView.getWidth());
        PropertyValuesHolder holder5 = PropertyValuesHolder.ofInt("rotation", 0, 90);
        PropertyValuesHolder holder6 = PropertyValuesHolder.ofInt("bottom", 0, 90);

//        int[] outLocation = new int[2];
//        view.getLocationOnScreen(outLocation);
//        view.setX(outLocation[0]);
//        view.setY(outLocation[1]);
        ValueAnimator valueAnimator = ObjectAnimator.ofPropertyValuesHolder(holder3, holder4, holder5, holder6);
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(-1, -1);
//                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(decorView.getHeight(), decorView.getWidth());
//                params.gravity = Gravity.CENTER;

                decorView.addView(view, params);
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

            }

            @Override
            public void onAnimationEnd(Animator animation) {
//                JZUtils.hideStatusBar(view.getContext());
//                JZUtils.hideSystemUI(view.getContext());
            }
        });
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Log.d(this.getClass().getSimpleName() + "-App", "onAnimationUpdate:getTop " + view.getBottom());
//                int width = (int) animation.getAnimatedValue("width");
//                int height = (int) animation.getAnimatedValue("height");
//                Log.d(this.getClass().getSimpleName() + "-App", "onAnimationUpdate: " + height);
//                ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
//                layoutParams.width = width;
//                layoutParams.height = height;
//                view.setLayoutParams(layoutParams);
//                int rotation = (int) animation.getAnimatedValue("rotation");
//                int bottom = (int) animation.getAnimatedValue("bottom");
//                view.setRotation(rotation);
//                view.setBottom(bottom);
            }
        });
        animatorSet.playTogether(valueAnimator);
        animatorSet.setDuration(800);
        animatorSet.start();
    }
}